﻿//计算机操作辅助类 
using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
//using System.Web.Security; 
//using System.Web.UI; 
//using System.Web.UI.WebControls; 
//using System.Web.UI.WebControls.WebParts; 
//using System.Web.UI.HtmlControls; 
using System.Management; 


/// <summary> 
/// 操作类型：ReStart 重启计算机 
/// Close关闭计算机 
/// ResetUser注销当前用户 
/// </summary> 
public enum OperatorType 
{ 
ReStart=0, 
Close=1, 
ResetUser=2 

} 
/// <summary> 
/// ComputerOperator 的摘要说明 
/// </summary> 
public class ComputerOperator
{
    private string UserName;
    private string UserPassword;
    private string UserIP;
    private string errorinfo;
    ConnectionOptions op;
    ManagementScope scope;
    System.Management.ObjectQuery oq;
    ManagementObjectCollection Query;
    public string ErrorInfo
    {
        get
        {
            return errorinfo;
        }
        private set
        {
            errorinfo = value;
        }
    }
    public ComputerOperator(string AdminName, string Password, string IP)
    {
        // 
        // TODO: 在此处添加构造函数逻辑 
        // 
        this.UserName = AdminName;
        this.UserPassword = Password;
        this.UserIP = IP;
    }

    private bool ConnectionComputer()
    {
        op = new ConnectionOptions();
        op.Username = this.UserName; //或者你的帐号（注意要有管理员的权限 
        op.Password = this.UserPassword; //你的密码 
        scope = new ManagementScope("\\\\" + this.UserIP + "\\root\\cimv2", op);
        try
        {
            //用给定管理者用户名和口令连接远程的计算机 
            scope.Connect();
            oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
            //得到WMI控制 
            this.Query = query1.Get();
            return true;
        }
        //报错 
        catch (Exception ee)
        {
            this.errorinfo = "连接" + this.UserIP + "出错，出错信息为：" + ee.Message;
            return false;
        }
    }

    /// <summary> 
    /// 执行计算机操作 
    /// </summary> 
    /// <param name="ot">操作类型</param> 
    /// <returns></returns> 
    public bool ExecutuOperator(OperatorType ot)
    {
        if (this.ConnectionComputer() == false)
        {
            return false;
        }
        else
        {
            try
            {
                foreach (ManagementObject mo in Query)
                {
                    string[] ss = { "", "" };
                    switch (ot)
                    {
                        case OperatorType.Close:
                            {
                                ss[0] = "12";
                                ss[1] = "0";
                                break;
                            }
                        case OperatorType.ResetUser:
                            {
                                ss[0] = "4";
                                ss[1] = "0";
                                break;
                            }
                        case OperatorType.ReStart:
                            {
                                ss[0] = "6";
                                ss[1] = "0";
                                break;
                            }
                    }
                    mo.InvokeMethod("win32ShutDown", ss);
                    // mo.InvokeMethod("Reboot", ss); 
                }
                return true;
            }
            catch (Exception ex)
            {
                this.errorinfo = ex.ToString();
                return false;
            }
        }
    }



}


